//
//  MineViewController.m
//  CaoShi
//
//  Created by aa on 2018/4/20.
//  Copyright © 2018年 杨旭. All rights reserved.
//

#import "YXMineViewController.h"
#import "YXMineHeaderView.h"
#import "HomeSortMenuView.h"
#import "YXLoginViewController.h"
#import "YXRegistViewController.h"
#import "YXPostListViewController.h"
#import "YXPostingViewController.h"
#import "YXIntegralViewController.h"
#import "YXSettingViewController.h"
@interface YXMineViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) YXMineHeaderView *mineHeaderView;
@property (nonatomic ,strong) HomeSortMenuView *menuView;
@property (nonatomic ,strong) UIView *headerView;
@property (nonatomic ,strong) NSArray *titleArr;
@property (nonatomic ,strong) NSArray *imageArr;
@property (nonatomic, strong) UIImageView *headImageView;
@property (nonatomic, assign) CGRect origialFrame;
@end

@implementation YXMineViewController

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:(CGRectMake(0, 0, kScreenWidth, kScreenHeight)) style:(UITableViewStylePlain)];
        _tableView.backgroundColor=[UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    }
    return _tableView;
}


-(UIImageView*)imageview{
    if (!_headImageView) {
        _headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 64+90)];
        _headImageView.image = [UIImage imageNamed:@"bg_zhuye"];
        self.origialFrame = _headImageView.frame;
    }
    return _headImageView;
}


- (HomeSortMenuView *)menuView {
    if (!_menuView) {
        _menuView = [[HomeSortMenuView alloc] initWithMineMoneySortMenuFrame:(CGRectMake(0, 184, kScreenWidth, 60))];
        YXWeakSelf
        [_menuView setMineMoneySortMenu:^(NSInteger index) {
            switch (index) {
                case 0:
                {
                    
                }
                    break;
                case 1:
                {
                    YXPostListViewController *vc = [[YXPostListViewController alloc] init];
                    vc.hidesBottomBarWhenPushed = YES;
                    [weakSelf.navigationController pushViewController:vc animated:YES];
                }
                    break;
                case 2:
                {
                    
                }
                    break;
                default:
                    break;
            }
            
        }];
        
    }
    return _menuView;
}

- (YXMineHeaderView *)mineHeaderView {
    if (!_mineHeaderView) {
        _mineHeaderView = [[YXMineHeaderView alloc] initWithFrame:(CGRectMake(0, 0, kScreenWidth, 184))];
        YXWeakSelf
        [_mineHeaderView setClickBtnBlock:^(NSInteger index) {
            if (index == 0 || index == 1) {
                
            }else if (index == 2) {
                YXLoginViewController *vc = [[YXLoginViewController alloc] init];
                UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
                [weakSelf presentViewController:nav animated:YES completion:nil];
            }else {
                YXRegistViewController *vc = [[YXRegistViewController alloc] init];
                [weakSelf presentViewController:vc animated:YES completion:nil];
            }
        }];
    }
    return _mineHeaderView;
}

- (NSArray *)titleArr {
    if (!_titleArr) {
        NSString *version = BunldVersion;
        _titleArr = @[@"我要发帖",@"我的地址",@"积分",@"设置",[NSString stringWithFormat:@"系统版本 V%@",version]];
    }
    return _titleArr;
}

- (NSArray *)imageArr {
    if (!_imageArr) {
        _imageArr = @[@"my_one",@"my_two",@"my_three",@"my_four",@"my_five"];
    }
    return _imageArr;
}

- (UIView *)headerView {
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, kScreenWidth, 244))];
        [_headerView addSubview:self.menuView];
        [_headerView addSubview:self.mineHeaderView];
    }
    return _headerView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.w_barBgAlpha = 0;
    AdjustsScrollViewInsetNever(self, self.tableView);
    
    [self.view addSubview:[self imageview]];

    [self.view addSubview:self.tableView];
    _tableView.tableHeaderView = self.headerView;
 
    
 

}

#pragma mark - UITableView Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titleArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kScreenWidth *44 / 375.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"mineCellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:cellID];
        cell.textLabel.font = [UIFont systemFontOfSize:15.0f];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    cell.textLabel.text = self.titleArr[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",self.imageArr[indexPath.row]]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        YXPostingViewController *vc = [[YXPostingViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.row == 1) {
        YXIntegralViewController *vc = [[YXIntegralViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.row == 3) {
        YXSettingViewController *vc = [[YXSettingViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}


#pragma mark - UIScrollerView Delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //往上滑动offset增加，往下滑动，yoffset减小
    CGFloat yoffset = scrollView.contentOffset.y;
    //处理背景图的放大效果和往上移动的效果
    if (yoffset>0) {//往上滑动
        
        _headImageView.frame = ({
            CGRect frame = self.origialFrame;
            frame.origin.y = self.origialFrame.origin.y - yoffset;
            frame;
        });
        
    }else {//往下滑动，放大处理
        _headImageView.frame = ({
            CGRect frame = self.origialFrame;
            frame.size.height = self.origialFrame.size.height - yoffset;
            frame.size.width = frame.size.height*4;
            frame.origin.x = _origialFrame.origin.x - (frame.size.width-_origialFrame.size.width)/2;
            frame;
        });
    }
}

@end
